package com.tjise.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjise.commonutils.JwtUtils;
import com.tjise.commonutils.R;
import com.tjise.commonutils.ordervo.CourseWebVoOrder;
import com.tjise.eduservice.client.OrderClient;
import com.tjise.eduservice.entity.EduCourse;
import com.tjise.eduservice.entity.EduTeacher;
import com.tjise.eduservice.entity.chapter.ChapterVo;
import com.tjise.eduservice.entity.frontvo.CourseFrontVo;
import com.tjise.eduservice.entity.frontvo.CourseWebVo;
import com.tjise.eduservice.service.EduChapterService;
import com.tjise.eduservice.service.EduCourseService;
import com.tjise.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")

@Api(description = "课程列表")
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    //1 条件查询带分页查询课程
    @ApiOperation("条件查询带分页查询课程")
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit, @RequestBody(required = false)CourseFrontVo courseFrontVo){
        Page<EduCourse> pageCourse = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCourseFrontList(pageCourse,courseFrontVo);
        //返回分页所有数据
        return R.ok().data(map);
    }

    //2 课程详情的方法
    @ApiOperation("课程详情的方法")
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        //根据课程id 编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);

        // 查询课程
        EduCourse course = courseService.getById(courseId);
        // 每点击一次就让他增加一个浏览量
        course.setViewCount(course.getViewCount()+1);
        // 增加浏览量
        courseService.updateById(course);

        //根据课程id查询章节和小节
        List<ChapterVo> chapterVedioList = chapterService.getChapterVoByCourseId(courseId);
        // 根据课程id和用户id查询当前课程是否已经支付过了

        // 获取请求头中的用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (memberId.equals("") || memberId.length() == 0  ) {
            // 没有登陆 返回登陆
            return R.error().code(28004).message("请登陆后再进行访问");
        }
        // 判断是否购买
        boolean buyCourse = orderClient.isBuyCourse(courseId, memberId);


        return R.ok().data("courseWebVo", courseWebVo).data("chapterVedioList", chapterVedioList).data("isBuy",buyCourse);
    }
    //根据课程id查询课程信息
    @ApiOperation(value = "根据课程id查询课程信息")
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo, courseWebVoOrder);
        return courseWebVoOrder;
    }
}
